/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Com.Sicovi.DAO;

import Com.Sicovi.Entidad.Area;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author darwin
 */
public class AreaDAO {
    private EntityManagerFactory emf;
    private EntityManager em;
    
    public AreaDAO(){
        emf=Persistence.createEntityManagerFactory("SICOVIPU");
        em=emf.createEntityManager();
    }
    
    public void CloseEntityManager(){
        em.close();
    }
    
    public boolean InsertarArea(Area a){
        boolean b=false;
        try{
            em.getTransaction().begin();
            em.persist(a);
            em.getTransaction().commit();
            b=true;
        }catch(Exception e){
            em.getTransaction().rollback();
        }
        return b; 
    }
    
   
     public List<Area> BuscarArea(String descripcion){
        Query q = em.createQuery("select a from Area a WHERE a.descArea='"+descripcion+"'");
        return q.getResultList();
    }
     
     public List<Area> getListarArea(){
        Query q = em.createQuery("select a from Area a WHERE a.estado=1");
       return q.getResultList();
    }
     
      public boolean  eliminaArea(int idArea){
        boolean b=false;
        try{
            em.getTransaction().begin();
            Query q = em.createQuery("UPDATE Area a SET a.estado=0 WHERE a.idArea="+idArea);
            q.executeUpdate();
            em.getTransaction().commit();
            b=true;
        }catch (Exception e){
            em.getTransaction().rollback();
        }
        return b;
    }    
}
